<?php
define("EW_PAGE_ID", "edit", TRUE); // Page ID
define("EW_TABLE_NAME", 'news', TRUE);
?>
<?php 
session_start(); // Initialize session data
ob_start(); // Turn on output buffering
?>
<?php include "ewcfg50.php" ?>
<?php include "ewmysql50.php" ?>
<?php include "phpfn50.php" ?>
<?php include "newsinfo.php" ?>
<?php include "userfn50.php" ?>
<?php include "usersinfo.php" ?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php

// Open connection to the database
$conn = ew_Connect();
?>
<?php
$Security = new cAdvancedSecurity();
?>
<?php
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
$Security->LoadCurrentUserLevel('news');
if (!$Security->IsLoggedIn()) {
	$Security->SaveLastUrl();
	Page_Terminate("login.php");
}
if (!$Security->CanEdit()) {
	$Security->SaveLastUrl();
	Page_Terminate("newslist.php");
}
?>
<?php

// Common page loading event (in userfn*.php)
Page_Loading();
?>
<?php

// Page load event, used in current page
Page_Load();
?>
<?php
$news->Export = @$_GET["export"]; // Get export parameter
$sExport = $news->Export; // Get export parameter, used in header
$sExportFile = $news->TableVar; // Get export file, used in header
?>
<?php

// Load key from QueryString
if (@$_GET["news_id"] <> "") {
	$news->news_id->setQueryStringValue($_GET["news_id"]);
}

// Create form object
$objForm = new cFormObj();
if (@$_POST["a_edit"] <> "") {
	$news->CurrentAction = $_POST["a_edit"]; // Get action code
	GetUploadFiles(); // Get upload files
	LoadFormValues(); // Get form values
} else {
	$news->CurrentAction = "I"; // Default action is display
}

// Check if valid key
if ($news->news_id->CurrentValue == "") Page_Terminate($news->getReturnUrl()); // Invalid key, exit
switch ($news->CurrentAction) {
	case "I": // Get a record to display
		if (!LoadRow()) { // Load Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
			Page_Terminate($news->getReturnUrl()); // Return to caller
		}
		break;
	Case "U": // Update
		$news->SendEmail = TRUE; // Send email on update success
		if (EditRow()) { // Update Record based on key
			$_SESSION[EW_SESSION_MESSAGE] = "Update successful"; // Update success
			Page_Terminate($news->getReturnUrl()); // Return to caller
		} else {
			RestoreFormValues(); // Restore form values if update failed
		}
}

// Render the record
$news->RowType = EW_ROWTYPE_EDIT; // Render as edit
RenderRow();
?>
<?php include "header.php" ?>
<script type="text/javascript">
<!--
var EW_PAGE_ID = "edit"; // Page id

//-->
</script>
<script type="text/javascript">
<!--

function ew_ValidateForm(fobj) {
	if (fobj.a_confirm && fobj.a_confirm.value == "F")
		return true;
	var i, elm, aelm, infix;
	var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
	for (i=0; i<rowcnt; i++) {
		infix = (fobj.key_count) ? String(i+1) : "";
		elm = fobj.elements["x" + infix + "_parent_id"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Parent ID"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_news_img"];
		if (elm && !ew_CheckFileType(elm.value)) { 
			if (!ew_OnError(elm, "File type is not allowed.")) 
				return false; 
		}
		elm = fobj.elements["x" + infix + "_file"];
		if (elm && !ew_CheckFileType(elm.value)) { 
			if (!ew_OnError(elm, "File type is not allowed.")) 
				return false; 
		}
		elm = fobj.elements["x" + infix + "_news_dateadd"];
		if (elm && !ew_HasValue(elm)) {
			if (!ew_OnError(elm, "Please enter required field - Date Add"))
				return false;
		}
		elm = fobj.elements["x" + infix + "_news_dateadd"];
		if (elm && !ew_CheckEuroDate(elm.value)) {
			if (!ew_OnError(elm, "Incorrect date, format = dd/mm/yyyy - Date Add"))
				return false; 
		}
	}
	return true;
}

//-->
</script>
<script type="text/javascript" src="fckeditor/fckeditor.js"></script>
<script type="text/javascript">
<!--
_width_multiplier = 16;
_height_multiplier = 60;
var ew_DHTMLEditors = [];

function ew_UpdateTextArea() {
	if (typeof ew_DHTMLEditors != 'undefined' &&
		typeof FCKeditorAPI != 'undefined') {			
			var inst;			
			for (inst in FCKeditorAPI.__Instances)
				FCKeditorAPI.__Instances[inst].UpdateLinkedField();
	}
}

//-->
</script>
<link rel="stylesheet" type="text/css" media="all" href="calendar/calendar-win2k-1.css" title="win2k-1" />
<script type="text/javascript" src="calendar/calendar.js"></script>
<script type="text/javascript" src="calendar/lang/calendar-en.js"></script>
<script type="text/javascript" src="calendar/calendar-setup.js"></script>
<script type="text/javascript">
<!--
var ew_MultiPagePage = "Page"; // multi-page Page Text
var ew_MultiPageOf = "of"; // multi-page Of Text
var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
var ew_MultiPageNext = "Next"; // multi-page Next Text

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your client script here, no need to add script tags.
// To include another .js script, use:
// ew_ClientScriptInclude("my_javascript.js"); 
//-->

</script>
<p><span class="phpmaker">Edit TABLE: news<br><br><a href="<?php echo $news->getReturnUrl() ?>">Go Back</a></span></p>
<?php
if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
<?php
	$_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
}
?>
<form name="fnewsedit" id="fnewsedit" action="newsedit.php" method="post" enctype="multipart/form-data">
<p>
<input type="hidden" name="a_edit" id="a_edit" value="U">
<table class="ewTable">
	<tr class="ewTableRow">
		<td class="ewTableHeader">News ID</td>
		<td<?php echo $news->news_id->CellAttributes() ?>><span id="cb_x_news_id">
<div<?php echo $news->news_id->ViewAttributes() ?>><?php echo $news->news_id->EditValue ?></div>
<input type="hidden" name="x_news_id" id="x_news_id" value="<?php echo ew_HtmlEncode($news->news_id->CurrentValue) ?>">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Parent ID<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $news->parent_id->CellAttributes() ?>><span id="cb_x_parent_id">
<select id="x_parent_id" name="x_parent_id" onChange="ew_AjaxUpdateOpt(this.form.x_sub_id, this, true);"<?php echo $news->parent_id->EditAttributes() ?>>
<!--option value="">Please Select</option-->
<?php
if (is_array($news->parent_id->EditValue)) {
	$arwrk = $news->parent_id->EditValue;
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($news->parent_id->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";	
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
			}
}
?>
</select>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Sub ID</td>
		<td<?php echo $news->sub_id->CellAttributes() ?>><span id="cb_x_sub_id">
<select id="x_sub_id" name="x_sub_id"<?php echo $news->sub_id->EditAttributes() ?>>
<!--option value="">Please Select</option-->
<?php
if (is_array($news->sub_id->EditValue)) {
	$arwrk = $news->sub_id->EditValue;
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = (strval($news->sub_id->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";	
?>
<option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
</option>
<?php
			}
}
?>
</select>
<?php
$sSqlWrk = "SELECT `sub_id`, `sub_name` FROM `subcat`";
$sSqlWrk .= " WHERE (`parent_id` IN (@FILTER_VALUE))";
$sSqlWrk .= " AND (" . "`sub_type`<>'3' AND `sub_type`<>'4'" . ")";
$sSqlWrk .= " ORDER BY `sub_order` Desc";
$sSqlWrk = TEAencrypt($sSqlWrk, EW_RANDOM_KEY);
?>
<input type="hidden" name="s_x_sub_id" id="s_x_sub_id" value="<?php echo $sSqlWrk ?>"><input type="hidden" name="lc_x_sub_id" id="lc_x_sub_id" value="2"><input type="hidden" name="ld1_x_sub_id" id="ld1_x_sub_id" value="1"><input type="hidden" name="ld2_x_sub_id" id="ld2_x_sub_id" value="-1"><input type="hidden" name="lft_x_sub_id" id="lft_x_sub_id" value="1">
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Title</td>
		<td<?php echo $news->news_title->CellAttributes() ?>><span id="cb_x_news_title">
<input type="text" name="x_news_title" id="x_news_title"  size="30" maxlength="250" value="<?php echo $news->news_title->EditValue ?>"<?php echo $news->news_title->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Image</td>
		<td<?php echo $news->news_img->CellAttributes() ?>><span id="cb_x_news_img">
<?php if (!is_null($news->news_img->Upload->DbValue)) { ?>
<input type="radio" name="a_news_img" id="a_news_img" value="1" checked>Keep&nbsp;
<input type="radio" name="a_news_img" id="a_news_img" value="2">Remove&nbsp;
<input type="radio" name="a_news_img" id="a_news_img" value="3">Replace<br>
<?php } else { ?>
<input type="hidden" name="a_news_img" id="a_news_img" value="3">
<?php } ?>
<input type="file" name="x_news_img" id="x_news_img"  size="30" onchange="if (this.form.a_news_img[2]) this.form.a_news_img[2].checked=true;"<?php echo $news->news_img->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Content</td>
		<td<?php echo $news->news_content->CellAttributes() ?>><span id="cb_x_news_content">
<textarea name="x_news_content" id="x_news_content" cols="35" rows="4"<?php echo $news->news_content->EditAttributes() ?>><?php echo $news->news_content->EditValue ?></textarea>
<script type="text/javascript">
<!--
var editor = new ew_DHTMLEditor("x_news_content");
editor.create = function() {
	var sBasePath = 'fckeditor/';
	var oFCKeditor = new FCKeditor('x_news_content', 35*_width_multiplier, 4*_height_multiplier);
	oFCKeditor.BasePath = sBasePath;
	oFCKeditor.ReplaceTextarea();
	this.active = true;
}
ew_DHTMLEditors[ew_DHTMLEditors.length] = editor;
-->
</script>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Detail</td>
		<td<?php echo $news->news_detail->CellAttributes() ?>><span id="cb_x_news_detail">
<textarea name="x_news_detail" id="x_news_detail" cols="35" rows="4"<?php echo $news->news_detail->EditAttributes() ?>><?php echo $news->news_detail->EditValue ?></textarea>
<script type="text/javascript">
<!--
var editor = new ew_DHTMLEditor("x_news_detail");
editor.create = function() {
	var sBasePath = 'fckeditor/';
	var oFCKeditor = new FCKeditor('x_news_detail', 35*_width_multiplier, 4*_height_multiplier);
	oFCKeditor.BasePath = sBasePath;
	oFCKeditor.ReplaceTextarea();
	this.active = true;
}
ew_DHTMLEditors[ew_DHTMLEditors.length] = editor;
-->
</script>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">File</td>
		<td<?php echo $news->file->CellAttributes() ?>><span id="cb_x_file">
<?php if (!is_null($news->file->Upload->DbValue)) { ?>
<input type="radio" name="a_file" id="a_file" value="1" checked>Keep&nbsp;
<input type="radio" name="a_file" id="a_file" value="2">Remove&nbsp;
<input type="radio" name="a_file" id="a_file" value="3">Replace<br>
<?php } else { ?>
<input type="hidden" name="a_file" id="a_file" value="3">
<?php } ?>
<input type="file" name="x_file" id="x_file"  size="30" onchange="if (this.form.a_file[2]) this.form.a_file[2].checked=true;"<?php echo $news->file->EditAttributes() ?>>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Date Add<span class='ewmsg'>&nbsp;*</span></td>
		<td<?php echo $news->news_dateadd->CellAttributes() ?>><span id="cb_x_news_dateadd">
<input type="text" name="x_news_dateadd" id="x_news_dateadd"  value="<?php echo $news->news_dateadd->EditValue ?>"<?php echo $news->news_dateadd->EditAttributes() ?>>
&nbsp;<img src="images/ew_calendar.gif" id="cx_news_dateadd" alt="Pick a Date" style="cursor:pointer;cursor:hand;">
<script type="text/javascript">
Calendar.setup(
{
inputField : "x_news_dateadd", // ID of the input field
ifFormat : "%d/%m/%Y", // the date format
button : "cx_news_dateadd" // ID of the button
}
);
</script>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Hot News</td>
		<td<?php echo $news->news_hot->CellAttributes() ?>><span id="cb_x_news_hot">
<?php
$arwrk = $news->news_hot->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($news->news_hot->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_news_hot[]" id="x_news_hot[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $news->news_hot->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
	<tr class="ewTableRow">
		<td class="ewTableHeader">Home Page</td>
		<td<?php echo $news->news_homepage->CellAttributes() ?>><span id="cb_x_news_homepage">
<?php
$arwrk = $news->news_homepage->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($news->news_homepage->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_news_homepage[]" id="x_news_homepage[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $news->news_homepage->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
	<tr class="ewTableAltRow">
		<td class="ewTableHeader">Active</td>
		<td<?php echo $news->news_active->CellAttributes() ?>><span id="cb_x_news_active">
<?php
$arwrk = $news->news_active->EditValue;
if (is_array($arwrk)) {
	$armultiwrk= explode(",", strval($news->news_active->CurrentValue));
	$rowswrk = count($arwrk);
	for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
		$selwrk = "";
		for ($ari = 0; $ari < count($armultiwrk); $ari++) {
			if (strval($arwrk[$rowcntwrk][0]) == trim(strval($armultiwrk[$ari]))) {
				$selwrk = " checked";
				break;
			}
		}
?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 1) ?>
<input type="checkbox" name="x_news_active[]" id="x_news_active[]" value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?><?php echo $news->news_active->EditAttributes() ?>>
<?php echo $arwrk[$rowcntwrk][1] ?>
<?php echo ew_RepeatColumnTable($rowswrk, $rowcntwrk, 5, 2) ?>
<?php
	}
}
?>
</span></td>
	</tr>
</table>
<p>
<input type="button" name="btnAction" id="btnAction" value="   Edit   " onClick="ew_SubmitForm(this.form);">
</form>
<script type="text/javascript">
<!--
ew_CreateEditor();  // Create DHTML editor(s)

//-->
</script>
<script language="JavaScript">
<!--
var f = document.fnewsedit;
ew_AjaxUpdateOpt(f.x_sub_id, f.x_parent_id, false);

//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--

// Write your table-specific startup script here
// document.write("page loaded");
//-->

</script>
<?php include "footer.php" ?>
<?php

// If control is passed here, simply terminate the page without redirect
Page_Terminate();

// -----------------------------------------------------------------
//  Subroutine Page_Terminate
//  - called when exit page
//  - clean up connection and objects
//  - if url specified, redirect to url, otherwise end response
function Page_Terminate($url = "") {
	global $conn;

	// Page unload event, used in current page
	Page_Unload();

	// Global page unloaded event (in userfn*.php)
	Page_Unloaded();

	 // Close Connection
	$conn->Close();

	// Go to url if specified
	if ($url <> "") {
		ob_end_clean();
		header("Location: $url");
	}
	exit();
}
?>
<?php

// Function Get upload files
function GetUploadFiles() {
	global $objForm, $news;

	// Get upload data
		if ($news->news_img->Upload->UploadFile()) {

			// No action required
		} else {
			echo $news->news_img->Upload->Message;
			exit();
		}
		if ($news->file->Upload->UploadFile()) {

			// No action required
		} else {
			echo $news->file->Upload->Message;
			exit();
		}
}
?>
<?php

// Load form values
function LoadFormValues() {

	// Load from form
	global $objForm, $news;
	$news->news_id->setFormValue($objForm->GetValue("x_news_id"));
	$news->parent_id->setFormValue($objForm->GetValue("x_parent_id"));
	$news->sub_id->setFormValue($objForm->GetValue("x_sub_id"));
	$news->news_title->setFormValue($objForm->GetValue("x_news_title"));
	$news->news_content->setFormValue($objForm->GetValue("x_news_content"));
	$news->news_detail->setFormValue($objForm->GetValue("x_news_detail"));
	$news->news_dateadd->setFormValue($objForm->GetValue("x_news_dateadd"));
	$news->news_dateadd->CurrentValue = ew_UnFormatDateTime($news->news_dateadd->CurrentValue, 7);
	$news->news_hot->setFormValue($objForm->GetValue("x_news_hot"));
	$news->news_homepage->setFormValue($objForm->GetValue("x_news_homepage"));
	$news->news_active->setFormValue($objForm->GetValue("x_news_active"));
}

// Restore form values
function RestoreFormValues() {
	global $news;
	$news->news_id->CurrentValue = $news->news_id->FormValue;
	$news->parent_id->CurrentValue = $news->parent_id->FormValue;
	$news->sub_id->CurrentValue = $news->sub_id->FormValue;
	$news->news_title->CurrentValue = $news->news_title->FormValue;
	$news->news_content->CurrentValue = $news->news_content->FormValue;
	$news->news_detail->CurrentValue = $news->news_detail->FormValue;
	$news->news_dateadd->CurrentValue = $news->news_dateadd->FormValue;
	$news->news_dateadd->CurrentValue = ew_UnFormatDateTime($news->news_dateadd->CurrentValue, 7);
	$news->news_hot->CurrentValue = $news->news_hot->FormValue;
	$news->news_homepage->CurrentValue = $news->news_homepage->FormValue;
	$news->news_active->CurrentValue = $news->news_active->FormValue;
}
?>
<?php

// Load row based on key values
function LoadRow() {
	global $conn, $Security, $news;
	$sFilter = $news->SqlKeyFilter();
	if (!is_numeric($news->news_id->CurrentValue)) {
		return FALSE; // Invalid key, exit
	}
	$sFilter = str_replace("@news_id@", ew_AdjustSql($news->news_id->CurrentValue), $sFilter); // Replace key value

	// Call Row Selecting event
	$news->Row_Selecting($sFilter);

	// Load sql based on filter
	$news->CurrentFilter = $sFilter;
	$sSql = $news->SQL();
	if ($rs = $conn->Execute($sSql)) {
		if ($rs->EOF) {
			$LoadRow = FALSE;
		} else {
			$LoadRow = TRUE;
			$rs->MoveFirst();
			LoadRowValues($rs); // Load row values

			// Call Row Selected event
			$news->Row_Selected($rs);
		}
		$rs->Close();
	} else {
		$LoadRow = FALSE;
	}
	return $LoadRow;
}

// Load row values from recordset
function LoadRowValues(&$rs) {
	global $news;
	$news->news_id->setDbValue($rs->fields('news_id'));
	$news->parent_id->setDbValue($rs->fields('parent_id'));
	$news->sub_id->setDbValue($rs->fields('sub_id'));
	$news->news_title->setDbValue($rs->fields('news_title'));
	$news->news_img->Upload->DbValue = $rs->fields('news_img');
	$news->news_content->setDbValue($rs->fields('news_content'));
	$news->news_detail->setDbValue($rs->fields('news_detail'));
	$news->file->Upload->DbValue = $rs->fields('file');
	$news->news_dateadd->setDbValue($rs->fields('news_dateadd'));
	$news->news_hot->setDbValue($rs->fields('news_hot'));
	$news->news_homepage->setDbValue($rs->fields('news_homepage'));
	$news->news_active->setDbValue($rs->fields('news_active'));
}
?>
<?php

// Render row values based on field settings
function RenderRow() {
	global $conn, $Security, $news;

	// Call Row Rendering event
	$news->Row_Rendering();

	// Common render codes for all row types
	// news_id

	$news->news_id->CellCssStyle = "";
	$news->news_id->CellCssClass = "";

	// parent_id
	$news->parent_id->CellCssStyle = "";
	$news->parent_id->CellCssClass = "";

	// sub_id
	$news->sub_id->CellCssStyle = "";
	$news->sub_id->CellCssClass = "";

	// news_title
	$news->news_title->CellCssStyle = "";
	$news->news_title->CellCssClass = "";

	// news_img
	$news->news_img->CellCssStyle = "";
	$news->news_img->CellCssClass = "";

	// news_content
	$news->news_content->CellCssStyle = "";
	$news->news_content->CellCssClass = "";

	// news_detail
	$news->news_detail->CellCssStyle = "";
	$news->news_detail->CellCssClass = "";

	// file
	$news->file->CellCssStyle = "";
	$news->file->CellCssClass = "";

	// news_dateadd
	$news->news_dateadd->CellCssStyle = "";
	$news->news_dateadd->CellCssClass = "";

	// news_hot
	$news->news_hot->CellCssStyle = "";
	$news->news_hot->CellCssClass = "";

	// news_homepage
	$news->news_homepage->CellCssStyle = "";
	$news->news_homepage->CellCssClass = "";

	// news_active
	$news->news_active->CellCssStyle = "";
	$news->news_active->CellCssClass = "";
	if ($news->RowType == EW_ROWTYPE_VIEW) { // View row
	} elseif ($news->RowType == EW_ROWTYPE_ADD) { // Add row
	} elseif ($news->RowType == EW_ROWTYPE_EDIT) { // Edit row

		// news_id
		$news->news_id->EditCustomAttributes = "";
		$news->news_id->EditValue = $news->news_id->CurrentValue;
		$news->news_id->CssStyle = "";
		$news->news_id->CssClass = "";
		$news->news_id->ViewCustomAttributes = "";

		// parent_id
		$news->parent_id->EditCustomAttributes = "";
		$sSqlWrk = "SELECT `parent_id`, `parent_name` FROM `parent`";
		$sSqlWrk .= " ORDER BY `parent_order` Desc";
		$rswrk = $conn->Execute($sSqlWrk);
		$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
		if ($rswrk) $rswrk->Close();
		array_unshift($arwrk, array("", "Please Select"));
		$news->parent_id->EditValue = $arwrk;

		// sub_id
		$news->sub_id->EditCustomAttributes = "";
		$sSqlWrk = "SELECT `sub_id`, `sub_name`, `parent_id` FROM `subcat`";
		if (trim(strval($news->sub_id->CurrentValue)) == "") {
			$sSqlWrk .= " WHERE 0=1";
		} else {
			$sSqlWrk .= " WHERE `sub_id` = " . ew_AdjustSql($news->sub_id->CurrentValue) . "";
		}
		$sSqlWrk .= " AND (" . "`sub_type`<>'3' AND `sub_type`<>'4'" . ")";
		$rswrk = $conn->Execute($sSqlWrk);
		$arwrk = ($rswrk) ? $rswrk->GetRows() : array();
		if ($rswrk) $rswrk->Close();
		array_unshift($arwrk, array("", "Please Select", ""));
		$news->sub_id->EditValue = $arwrk;

		// news_title
		$news->news_title->EditCustomAttributes = "";
		$news->news_title->EditValue = ew_HtmlEncode($news->news_title->CurrentValue);

		// news_img
		$news->news_img->EditCustomAttributes = "";
		$news->news_img->EditValue = $news->news_img->CurrentValue;

		// news_content
		$news->news_content->EditCustomAttributes = "";
		$news->news_content->EditValue = ew_HtmlEncode($news->news_content->CurrentValue);

		// news_detail
		$news->news_detail->EditCustomAttributes = "";
		$news->news_detail->EditValue = ew_HtmlEncode($news->news_detail->CurrentValue);

		// file
		$news->file->EditCustomAttributes = "";
		$news->file->EditValue = $news->file->CurrentValue;

		// news_dateadd
		$news->news_dateadd->EditCustomAttributes = "";
		$news->news_dateadd->EditValue = ew_HtmlEncode(ew_FormatDateTime($news->news_dateadd->CurrentValue, 7));

		// news_hot
		$news->news_hot->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$news->news_hot->EditValue = $arwrk;

		// news_homepage
		$news->news_homepage->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Home Page");
		$news->news_homepage->EditValue = $arwrk;

		// news_active
		$news->news_active->EditCustomAttributes = "";
		$arwrk = array();
		$arwrk[] = array("1", "Active");
		$news->news_active->EditValue = $arwrk;
	} elseif ($news->RowType == EW_ROWTYPE_SEARCH) { // Search row
	}

	// Call Row Rendered event
	$news->Row_Rendered();
}
?>
<?php

// Update record based on key values
function EditRow() {
	global $conn, $Security, $news;
	$sFilter = $news->SqlKeyFilter();
	if (!is_numeric($news->news_id->CurrentValue)) {
		return FALSE;
	}
	$sFilter = str_replace("@news_id@", ew_AdjustSql($news->news_id->CurrentValue), $sFilter); // Replace key value
	$news->CurrentFilter = $sFilter;
	$sSql = $news->SQL();
	$conn->raiseErrorFn = 'ew_ErrorFn';
	$rs = $conn->Execute($sSql);
	$conn->raiseErrorFn = '';
	if ($rs === FALSE)
		return FALSE;
	if ($rs->EOF) {
		$EditRow = FALSE; // Update Failed
	} else {

		// Save old values
		$rsold =& $rs->fields;
		$rsnew = array();

		// Field news_id
		// Field parent_id

		$news->parent_id->SetDbValueDef($news->parent_id->CurrentValue, 0);
		$rsnew['parent_id'] =& $news->parent_id->DbValue;

		// Field sub_id
		$news->sub_id->SetDbValueDef($news->sub_id->CurrentValue, NULL);
		$rsnew['sub_id'] =& $news->sub_id->DbValue;

		// Field news_title
		$news->news_title->SetDbValueDef($news->news_title->CurrentValue, NULL);
		$rsnew['news_title'] =& $news->news_title->DbValue;

		// Field news_img
		$news->news_img->Upload->SaveToSession(); // Save file value to Session
		if ($news->news_img->Upload->Action == "2" || $news->news_img->Upload->Action == "3") { // Update/Remove
		$news->news_img->DbValue = $rs->fields('news_img'); // Get original value
		if (is_null($news->news_img->Upload->Value)) {
			$news->news_img->Upload->DbValue = NULL;
		} else {
			$news->news_img->Upload->DbValue = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $news->news_img->Upload->FileName);
		}
		$rsnew['news_img'] =& $news->news_img->Upload->DbValue;
		}

		// Field news_content
		$news->news_content->SetDbValueDef($news->news_content->CurrentValue, NULL);
		$rsnew['news_content'] =& $news->news_content->DbValue;

		// Field news_detail
		$news->news_detail->SetDbValueDef($news->news_detail->CurrentValue, NULL);
		$rsnew['news_detail'] =& $news->news_detail->DbValue;

		// Field file
		$news->file->Upload->SaveToSession(); // Save file value to Session
		if ($news->file->Upload->Action == "2" || $news->file->Upload->Action == "3") { // Update/Remove
		$news->file->DbValue = $rs->fields('file'); // Get original value
		if (is_null($news->file->Upload->Value)) {
			$news->file->Upload->DbValue = NULL;
		} else {
			$news->file->Upload->DbValue = ew_UploadFileNameEx(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $news->file->Upload->FileName);
		}
		$rsnew['file'] =& $news->file->Upload->DbValue;
		}

		// Field news_dateadd
		$news->news_dateadd->SetDbValueDef(ew_UnFormatDateTime($news->news_dateadd->CurrentValue, 7), ew_CurrentDate());
		$rsnew['news_dateadd'] =& $news->news_dateadd->DbValue;

		// Field news_hot
		$news->news_hot->SetDbValueDef($news->news_hot->CurrentValue, NULL);
		$rsnew['news_hot'] =& $news->news_hot->DbValue;

		// Field news_homepage
		$news->news_homepage->SetDbValueDef($news->news_homepage->CurrentValue, NULL);
		$rsnew['news_homepage'] =& $news->news_homepage->DbValue;

		// Field news_active
		$news->news_active->SetDbValueDef($news->news_active->CurrentValue, NULL);
		$rsnew['news_active'] =& $news->news_active->DbValue;

		// Call Row Updating event
		$bUpdateRow = $news->Row_Updating($rsold, $rsnew);
		if ($bUpdateRow) {

		// Field news_img
			if (!is_null($news->news_img->Upload->Value)) ew_SaveFile(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $rsnew['news_img'], $news->news_img->Upload->Value);

		// Field file
			if (!is_null($news->file->Upload->Value)) ew_SaveFile(ew_UploadPathEx(TRUE, EW_UPLOAD_DEST_PATH), $rsnew['file'], $news->file->Upload->Value);
			$conn->raiseErrorFn = 'ew_ErrorFn';
			$EditRow = $conn->Execute($news->UpdateSQL($rsnew));
			$conn->raiseErrorFn = '';
		} else {
			if ($news->CancelMessage <> "") {
				$_SESSION[EW_SESSION_MESSAGE] = $news->CancelMessage;
				$news->CancelMessage = "";
			} else {
				$_SESSION[EW_SESSION_MESSAGE] = "Update cancelled";
			}
			$EditRow = FALSE;
		}
	}

	// Call Row Updated event
	if ($EditRow) {
		$news->Row_Updated($rsold, $rsnew);
	}
	$rs->Close();

	// Field news_img
	$news->news_img->Upload->RemoveFromSession(); // Remove file value from Session

	// Field file
	$news->file->Upload->RemoveFromSession(); // Remove file value from Session
	return $EditRow;
}
?>
<?php

// Page Load event
function Page_Load() {

	//echo "Page Load";
}

// Page Unload event
function Page_Unload() {

	//echo "Page Unload";
}
?>
